//
// Copyright 2011 ReedRex Corp
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Queries.m
// Cloud BookShelf
//
// Created by Shunichi Takagi on 11/02/25.
//

#import "Queries.h"


@implementation Queries

@synthesize filterString;

- (id) initWithString:(NSString *)aFilterString {
	self.filterString = aFilterString;
	
	return self;
}

+ (Queries *) withEmpty {
	return [[[Queries alloc] initWithString:@""] autorelease];
}

+ (Queries *) with:(NSString *)name eq:(NSString *)value {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ eq '%@')", name, [Utility URLEncode:value]]] autorelease];
}

+ (Queries *) with:(NSString *)name gt:(NSString *)value {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ gt '%@')", name, [Utility URLEncode:value]]] autorelease];
}

+ (Queries *) with:(NSString *)name ge:(NSString *)value {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ ge '%@')", name, [Utility URLEncode:value]]] autorelease];
}

+ (Queries *) with:(NSString *)name lt:(NSString *)value {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ lt '%@')", name, [Utility URLEncode:value]]] autorelease];
}

+ (Queries *) with:(NSString *)name le:(NSString *)value {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ le '%@')", name, [Utility URLEncode:value]]] autorelease];
}

+ (Queries *) with:(NSString *)name ne:(NSString *)value {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ ne '%@')", name, [Utility URLEncode:value]]] autorelease];
}

- (Queries *) and:(Queries *)aQueries {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ and %@)", self.filterString, aQueries.filterString]] autorelease];
}

- (Queries *) or:(Queries *)aQueries {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(%@ or %@)", self.filterString, aQueries.filterString]] autorelease];
}

- (Queries *) not {
	return [[[Queries alloc] initWithString:[NSString stringWithFormat:@"(not %@)", self.filterString]] autorelease];	
}

- (void) dealloc {
	[self.filterString release];
	
	[super dealloc];
}

@end
